package m.fun.service;

import m.common.model.util.ModelCheckUtil;
import m.common.model.util.ModelUpdate;
import m.common.model.util.ModelUpdateUtil;
import m.common.model.util.QueryCondition;
import m.common.service.Service;
import m.fun.model.OfferDetail;
import m.fun.model.OfferInfo;
import m.system.exception.MException;
import m.system.util.GenerateID;
import m.system.util.ObjectUtil;

import java.sql.SQLException;

public class OfferInfoService extends Service {
    public String save(OfferInfo model) throws MException, SQLException {
        String msg;
        ModelCheckUtil.checkNotNull(model,"title","contact","tel","address");
        ModelCheckUtil.checkUniqueCombine(model,new String[]{"title","user.oid"},"报价单标题已存在");
        if(ObjectUtil.isEmptyOid(model)){
            model.setOid(GenerateID.generatePrimaryKey());
            model.setViewNum(0);
            ModelUpdateUtil.insertModel(model);
            msg="保存成功";
        }else{
            ModelUpdateUtil.updateModel(model,"title","description","contact","tel","address");
            msg="修改成功";
        }
        return msg;
    }
    public void updateDetailNum(String oid) throws MException {
        ModelUpdate.init(OfferInfo.class)
            .set("detailNum", OfferDetail.class,"count(#{oid})",
                    QueryCondition.eq("offer.oid",oid))
            .execute(oid);
    }
}
